/**
 * 生命密码解析说明：
 * 用户输入生日，例如：1997年01月27日
 * - 月份若为个位数需补全为两位数，如1月应为"01"
 * - 日期若为个位数需补全为两位数，如1日应为"01"
 * - 例如1997年01月27日，格式化后为："27-01-1997"
 * - 格式化后的生日标记为：AB-CD-EFGH
 *   其中：
 *     A = 2, B = 7, C = 0, D = 1, E = 1, F = 9, G = 9, H = 7
 * 用户输入生日， 例如：2021年10月01日
 * - 格式化后为："01-10-2021"
 * - 格式化后的生日标记为：AB-CD-EFGH
 *   其中：
 *     A = 0, B = 1, C = 1, D = 0, E = 2, F = 0, G = 2, H = 1
 *
 * 函数 formatTime 用于格式化时间，返回字符串格式为"YYYY/MM/DD HH:MM:SS"
 * 其中YYYY为四位年份，MM为两位月份，DD为两位
 *
 * 函数 formatNumber 用于格式化数字，若数字小于10，则在前面补0
 *
 * 函数 mergeNumber 用于合并两个数字，若和大于9，则将十位和个位相加返回，否则直接返回和
 *
 * 函数 getA 到 getH 用于获取生日标记的各个部分
 * 例如 getA(date) 返回日期的十位数，getB(date) 返回日期的个位数
 * 函数 getI 到 getX 用于获取生日标记的合并结果
 * 例如 getI(date) 返回 A 和 B 的合并结果，getJ(date) 返回 C 和 D 的合并结果
 */

export const formatTime = (date: Date) => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();

    return [year, month, day].map(formatNumber).join("/") + " " + [hour, minute, second].map(formatNumber).join(":");
};

const formatNumber = (n: number) => {
    const s = n.toString();
    return s[1] ? s : "0" + s;
};
export const mergeNumber = (num1: number, num2: number): number => {
    const sum = num1 + num2;
    if (sum > 9) {
        const tens = Math.floor(sum / 10);
        const ones = sum % 10;
        return tens + ones;
    } else {
        return sum;
    }
};
export const getA = (date: string): number => {
    const dateArray = date.split("-");
    const day = dateArray[2];
    const iDay = parseInt(day);
    return Math.floor(iDay / 10);
};
export const getB = (date: string): number => {
    const dateArray = date.split("-");
    const day = dateArray[2];
    const iDay = parseInt(day);
    return iDay % 10;
};
export const getC = (date: string): number => {
    const dateArray = date.split("-");
    const month = dateArray[1];
    const iMonth = parseInt(month);
    return Math.floor(iMonth / 10);
};
export const getD = (date: string): number => {
    const dateArray = date.split("-");
    const month = dateArray[1];
    const iMonth = parseInt(month);
    return iMonth % 10;
};
export const getE = (date: string): number => {
    const dateArray = date.split("-");
    const year = dateArray[0];
    const iYear = parseInt(year);
    const yearHundred = Math.floor(iYear / 100);
    return Math.floor(yearHundred / 10);
};
export const getF = (date: string): number => {
    const dateArray = date.split("-");
    const year = dateArray[0];
    const iYear = parseInt(year);
    const yearHundred = Math.floor(iYear / 100);
    return yearHundred % 10;
};
export const getG = (date: string): number => {
    const dateArray = date.split("-");
    const year = dateArray[0];
    const iYear = parseInt(year);
    const yearHundred = iYear % 100;
    return Math.floor(yearHundred / 10);
};
export const getH = (date: string): number => {
    const dateArray = date.split("-");
    const year = dateArray[0];
    const iYear = parseInt(year);
    const yearHundred = iYear % 100;
    return yearHundred % 10;
};
export const getI = (date: string): number => {
    return mergeNumber(getA(date), getB(date));
};
export const getJ = (date: string): number => {
    return mergeNumber(getC(date), getD(date));
};
export const getK = (date: string): number => {
    return mergeNumber(getE(date), getF(date));
};
export const getL = (date: string): number => {
    return mergeNumber(getG(date), getH(date));
};
export const getM = (date: string): number => {
    return mergeNumber(getI(date), getJ(date));
};
export const getN = (date: string): number => {
    return mergeNumber(getK(date), getL(date));
};
export const getO = (date: string): number => {
    return mergeNumber(getM(date), getN(date));
};
export const getP = (date: string): number => {
    return mergeNumber(getM(date), getO(date));
};
export const getQ = (date: string): number => {
    return mergeNumber(getN(date), getO(date));
};
export const getR = (date: string): number => {
    return mergeNumber(getQ(date), getP(date));
};
export const getS = (date: string): number => {
    return mergeNumber(getJ(date), getM(date));
};
export const getT = (date: string): number => {
    return mergeNumber(getI(date), getM(date));
};
export const getU = (date: string): number => {
    return mergeNumber(getT(date), getS(date));
};
export const getV = (date: string): number => {
    return mergeNumber(getK(date), getN(date));
};
export const getW = (date: string): number => {
    return mergeNumber(getL(date), getN(date));
};
export const getX = (date: string): number => {
    return mergeNumber(getV(date), getW(date));
};
